package com.zimmur.app.mocolink.background.util;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.zimmur.app.mocolink.background.common.PropertiesCommon;
import com.zimmur.app.mocolink.domain.util.DateUtils;
import com.zimmur.app.mocolink.domain.util.StringUtils;

public class WanYiNoteUtil {
	private static Logger log = LoggerFactory.getLogger(WanYiNoteUtil.class);
	private static final  String sendNoteUrl="https://api.netease.im/sms/sendcode.action";
	private static final  String checkNoteUrl="https://api.netease.im/sms/verifycode.action";
	private static final  String operationNoteUrl="https://api.netease.im/sms/sendtemplate.action";
	private static  String appKey;
	private static  String appSecret;
	private static  String registerModelId;
	private static  String changeModelId;
	private static  String createOrderModelId;
	private static  String createOrderParam;
	public static final String SuccessCode="200";//发送成功
	public static final String SendTooMoreCode="419";//当天发送短信次数超过限制
	//错误返回码
	public static final String SendTooMoreError="SendTooMore";
	public static final String UnknowError="UnknowError";
	static{
		appKey=PropertiesCommon.getProperty("wyNote.appKey");
		appSecret=PropertiesCommon.getProperty("wyNote.appSecret");
		registerModelId=PropertiesCommon.getProperty("wyNote.registerModelId");
		changeModelId=PropertiesCommon.getProperty("wyNote.changeModelId");
		createOrderModelId=PropertiesCommon.getProperty("wyNote.createOrderModelId");
		createOrderParam=PropertiesCommon.getProperty("wyNote.createOrderParam");
	}
	/**
	 * 发送短信（网易云信）
	 * @param mobile	手机号
	 * @param type		短信类型：1:注册/2:修改信息
	 */	
	public static String sendNote(String mobile,Integer type){
		String time=DateUtils.getNowUnixTime()+"";
		String nonce=UUID.randomUUID().toString().replace("-", "");
		String checkSum=CheckSumBuilder.getCheckSum(appSecret, nonce, time);
		List<NameValuePair> params=new ArrayList<>();
		params.add(new BasicNameValuePair("mobile", mobile));
		if(type==1){
			params.add(new BasicNameValuePair("templateid", registerModelId));
		}else{
			params.add(new BasicNameValuePair("templateid", changeModelId));
		}
		List<NameValuePair> headers = new ArrayList<NameValuePair>();
		headers.add(new BasicNameValuePair("AppKey", appKey));
		headers.add(new BasicNameValuePair("Nonce", nonce));
		headers.add(new BasicNameValuePair("CheckSum", checkSum));
		headers.add(new BasicNameValuePair("CurTime", DateUtils.getNowUnixTime()+""));
		headers.add(new BasicNameValuePair("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"));
		String strRet = HttpClientUtil.doPost(sendNoteUrl, params, headers);
		log.debug(strRet);
	
		JSONObject object=JSONObject.parseObject(strRet);
		if(SuccessCode.equals(object.getString("code"))){
			return object.getString("obj");
		}else if(SendTooMoreCode.equals(object.getString("code"))){
			return SendTooMoreError;
		}else{
			return UnknowError;
		}
	}
	
	public static boolean checkNote(String mobile,String code){
		String time=DateUtils.getNowUnixTime()+"";
		String nonce=UUID.randomUUID().toString().replace("-", "");
		String checkSum=CheckSumBuilder.getCheckSum(appSecret, nonce, time);
		List<NameValuePair> params=new ArrayList<>();
		params.add(new BasicNameValuePair("mobile", mobile));
		params.add(new BasicNameValuePair("code", code));
		List<NameValuePair> headers = new ArrayList<NameValuePair>();
		headers.add(new BasicNameValuePair("AppKey", appKey));
		headers.add(new BasicNameValuePair("Nonce", nonce));
		headers.add(new BasicNameValuePair("CheckSum", checkSum));
		headers.add(new BasicNameValuePair("CurTime", DateUtils.getNowUnixTime()+""));
		headers.add(new BasicNameValuePair("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"));
		String strRet = HttpClientUtil.doPost(checkNoteUrl, params, headers);
		log.debug(strRet);
		JSONObject object=JSONObject.parseObject(strRet);
		String result=object.getString("code");
		return "200".equals(result);
	}
	/**
	 * 发送网易云运营类短信
	 *@Description
	 *@param mobile
	 *@param param
	 *@return 
	 *@author zcx
	 *@Time 2017年10月23日 下午2:34:27
	 */
	public static boolean sendOperationNote(String mobile){
		String time=DateUtils.getNowUnixTime()+"";
		String nonce=UUID.randomUUID().toString().replace("-", "");
		String checkSum=CheckSumBuilder.getCheckSum(appSecret, nonce, time);
		List<NameValuePair> params=new ArrayList<>();
		mobile = "[\""+mobile+"\"]";
		params.add(new BasicNameValuePair("mobiles", mobile));
		params.add(new BasicNameValuePair("templateid", createOrderModelId));
		params.add(new BasicNameValuePair("params", createOrderParam));
		List<NameValuePair> headers = new ArrayList<NameValuePair>();
		headers.add(new BasicNameValuePair("AppKey", appKey));
		headers.add(new BasicNameValuePair("Nonce", nonce));
		headers.add(new BasicNameValuePair("CheckSum", checkSum));
		headers.add(new BasicNameValuePair("CurTime", DateUtils.getNowUnixTime()+""));
		headers.add(new BasicNameValuePair("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"));
		String strRet = HttpClientUtil.doPost(operationNoteUrl, params, headers);
		log.debug(strRet);
		if(!StringUtils.isBlank(strRet)){
			JSONObject object=JSONObject.parseObject(strRet);
			String result=object.getString("code");
			return "200".equals(result);
		}
		return false;
	}
	public static void main(String[] args) {
		boolean code=sendOperationNote("15201137527");
		System.out.println(code);
		/*if(checkNote("15201137527","1044")){
			System.out.println("成功");
		}else{
			System.out.println("失败");
		}*/
	}
}
